
export function init(){
    // 标签页切换
    document.querySelectorAll('.tab-item').forEach(tab => {
        tab.addEventListener('click', function() {
            // 更新标签页状态
            document.querySelectorAll('.tab-item').forEach(t => t.classList.remove('active'));
            this.classList.add('active');

            // 更新内容区域
            const tabId = this.dataset.tab;
            document.querySelectorAll('.tab-content').forEach(content => {
                content.classList.remove('active');
            });
            document.getElementById(`${tabId}-tab`).classList.add('active');
        });
    });

    // 图标选择
    document.querySelectorAll('.icon-option').forEach(icon => {
        icon.addEventListener('click', function() {
            document.querySelectorAll('.icon-option').forEach(i => i.classList.remove('selected'));
            this.classList.add('selected');
        });
    });

    // 颜色选择
    document.querySelectorAll('.color-option').forEach(color => {
        color.addEventListener('click', function() {
            document.querySelectorAll('.color-option').forEach(c => c.classList.remove('selected'));
            this.classList.add('selected');
        });
    });

    // 添加权限按钮
    document.getElementById('addPermissionBtn').addEventListener('click', function() {
        document.getElementById('addPermissionForm').style.display = 'flex';
        this.style.display = 'none';
    });

    document.getElementById('cancelAddPermission').addEventListener('click', function() {
        document.getElementById('addPermissionForm').style.display = 'none';
        document.getElementById('addPermissionBtn').style.display = 'block';
    });

    // 复制API Key
    document.getElementById('copyApiKeyBtn').addEventListener('click', function() {
        const apiKey = document.querySelector('.api-key-value').textContent;
        navigator.clipboard.writeText(apiKey).then(() => {
            alert('API Key已复制到剪贴板');
        });
    });

    // 重新生成API Key
    document.getElementById('regenerateApiKeyBtn').addEventListener('click', function() {
        if (confirm('确定要重新生成API Key吗？现有的Key将立即失效。')) {
            const newKey = 'sk_' + Math.random().toString(36).substring(2, 18) + Math.random().toString(36).substring(2, 18);
            document.querySelector('.api-key-value').textContent = newKey;
            alert('API Key已重新生成');
        }
    });

    // 重置设置
    document.getElementById('resetSettingsBtn').addEventListener('click', function() {
        document.getElementById('resetModal').style.display = 'flex';
    });

    document.getElementById('closeResetModal').addEventListener('click', function() {
        document.getElementById('resetModal').style.display = 'none';
    });

    document.getElementById('cancelResetBtn').addEventListener('click', function() {
        document.getElementById('resetModal').style.display = 'none';
    });

    document.getElementById('confirmResetBtn').addEventListener('click', function() {
        alert('应用设置已重置为默认值');
        document.getElementById('resetModal').style.display = 'none';
    });

    // 删除应用
    document.getElementById('deleteAppBtn').addEventListener('click', function() {
        document.getElementById('deleteModal').style.display = 'flex';
    });

    document.getElementById('closeDeleteModal').addEventListener('click', function() {
        document.getElementById('deleteModal').style.display = 'none';
    });

    document.getElementById('cancelDeleteBtn').addEventListener('click', function() {
        document.getElementById('deleteModal').style.display = 'none';
    });

    document.getElementById('confirmDeleteInput').addEventListener('input', function() {
        document.getElementById('confirmDeleteBtn').disabled =
            this.value !== '公文写作';
    });

    document.getElementById('confirmDeleteBtn').addEventListener('click', function() {
        alert('应用删除功能将在实际应用中实现');
        document.getElementById('deleteModal').style.display = 'none';
    });

    // 保存按钮
    document.getElementById('saveBtn').addEventListener('click', function() {
        alert('设置已保存');
    });

    document.getElementById('cancelBtn').addEventListener('click', function() {
        if (confirm('您有未保存的更改，确定要取消吗？')) {
            alert('返回上一页功能将在实际应用中实现');
        }
    });

    // 返回按钮
    document.getElementById('backBtn').addEventListener('click', function() {
        if (confirm('您有未保存的更改，确定要返回吗？')) {
            alert('返回上一页功能将在实际应用中实现');
        }
    });
};